#include "Compilation.h"
#include <fstream>

#include <iostream>
using namespace std;

void Compilation::Compile() {
	if (_root()->IsParent()) {
		for (std::vector<SP<ParseNode> >::iterator it = ((ParentNode*)_root())->children.begin(); it < ((ParentNode*)_root())->children.end(); ++it)
			(**it).parent = ((ParentNode*)_root());
	}
	
	EnvironmentBuildingVisitor ebv;
	Accept(&ebv);
	
	

	error = (ErrorCode)(EnvironmentBuilding * ebv.error);
	if (error) return;

	TypeLinkingVisitor tlv;
	Accept(&tlv);

	
	error = (ErrorCode)(TypeLinking * tlv.error);
	if (error) return;

	HierarchyCheckingVisitor hcv;
	Accept(&hcv);

	
	error = (ErrorCode)(HierarchyChecking * hcv.error);
	if (error) return;

	//EnvironmentVisitor ev;
	//Accept(&ev);

	DisambiguateVisitor dv;
	Accept(&dv);

	
	error = (ErrorCode)(Disambiguation * dv.error);
	if (error) return;

	TypeCheckVisitor tcv;
	Accept(&tcv);

	
	error = (ErrorCode)(TypeChecking * tcv.error);
	if (error) return;

	ReachabilityTreeRestucturer rtr;
	Accept(&rtr);
	ReachabilityVisitor rv;
	Accept(&rv);

	error = (ErrorCode)(Reachability * rv.error);
	if (error) return;
	
	LeafDestroyerVisitor ldv;
	Accept(&ldv);

	IndexVisitor iv;
	Accept(&iv);

	CodeGeneratingVisitor cgv(iv);
	Accept(&cgv);

	error = (ErrorCode)(CodeGeneration * cgv.error);
	if (error) return;
	
	//PrintVisitor pv;
	//Accept(&pv);
}